package com.magoldeditor.controller;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;

import com.magoldeditor.model.IMap;
import com.magoldeditor.view.IEditor;

public class MapHandler extends Handler implements KeyListener {
	
	@SuppressWarnings("unused")
	private boolean isMoving = false;
	private IMap map = null;

	public MapHandler(IEditor editor, IMap map) {
		super(editor);
		this.setMap(map);
		editor.addPainter(map);
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		this.map.toEntry(e.getX(), e.getY());
		this.editor.focusOn();
	}
	
	@Override
	public void mouseMoved(MouseEvent e) {
		this.map.toMove(e.getX(), e.getY());
	}
	
	@Override
	public void mouseReleased(MouseEvent e) {
		this.map.toMove(e.getX(), e.getY());
	}
	
	@Override
	public void mouseDragged(MouseEvent e) {
		this.map.toRotate(e.getX(), e.getY());
	}
	
	@Override
	public void mouseWheelMoved(MouseWheelEvent e) {
		this.map.toEnlarge(e.getWheelRotation());
	}
	
	public IMap getMap() {
		return map;
	}

	public void setMap(IMap map) {
		if (map != null) this.map = map;
		else throw new IllegalArgumentException("the map can not be null");
	}

	@Override
	public void keyPressed(KeyEvent e) {
		
		switch (e.getKeyCode()) {
		case KeyEvent.VK_W:
			this.map.toUp();
			break;
		case KeyEvent.VK_A:
			this.map.toLeft();
			break;
		case KeyEvent.VK_S:
			this.map.toDown();
			break;
		case KeyEvent.VK_D:
			this.map.toRight();
			break;
			default:
				System.err.println("pressed wrong key");
				break;
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		
	}
}
